package gui;

import java.awt.EventQueue;
import java.awt.image.BufferedImage;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JFrame;

import com.xuggle.xuggler.ICodec;
import com.xuggle.xuggler.IContainer;
import com.xuggle.xuggler.IPacket;
import com.xuggle.xuggler.IPixelFormat;
import com.xuggle.xuggler.IStream;
import com.xuggle.xuggler.IStreamCoder;
import com.xuggle.xuggler.IVideoPicture;
import com.xuggle.xuggler.IVideoResampler;

public class MainForm {

	private JFrame frame;
	private ListenerThread listener = new ListenerThread();
	private ConnectionThread conn = new ConnectionThread();
	private ServerSocket server = null;
	private Socket client = null;
	private DataOutputStream out = null;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainForm window = new MainForm();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainForm() {
		initialize();
		listener.setMainForm(this);
		try {
			server = new ServerSocket(4444);
			conn.setServerSocket(server);
			conn.setMainForm(this);
			conn.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void initClient(Socket client){
		try {
			this.client = client;
			out = new DataOutputStream(client.getOutputStream());
			int videoStreamId = -1;
			IStreamCoder videoCoder = null;
			
			IContainer c = IContainer.make();
			//"C:\\roboguideWorkspace\\a.avi", IContainer.Type.READ,
			c.open("C:\\roboguideWorkspace\\a.avi", IContainer.Type.READ, null);
			for(int i = 0; i < c.getNumStreams(); i++){
				IStream s = c.getStream(i);
				IStreamCoder coder = s.getStreamCoder();
				
				if (coder.getCodecType() == ICodec.Type.CODEC_TYPE_VIDEO){
					videoStreamId = i;
					videoCoder = coder;
					break;
				}
			}
			videoCoder.open();
			IPacket p = IPacket.make();
			
			while(c.readNextPacket(p) >= 0){
				if(p.getStreamIndex() == videoStreamId){
					byte[] sending = p.getByteBuffer().array();
					System.out.println(sending);
//					out.write(sending);
//					out.flush();
//					IVideoPicture pic = IVideoPicture.make(videoCoder.getPixelType(),videoCoder.getWidth(),videoCoder.getHeight());
//					resampler = IVideoResampler.make(videoCoder.getWidth(),videoCoder.getHeight(), IPixelFormat.Type.RGB24, videoCoder.getWidth(),videoCoder.getHeight(), pic.getPixelType());
//					int offset = 0;
//					while (offset < p.getSize()){
//						int bytesDecoded = videoCoder.decodeVideo(pic, p, offset);
//						if(bytesDecoded < 0){
//							
//						} else {
//							offset += bytesDecoded;
//						}
//						
//						if (pic.isComplete()){
//							IVideoPicture newPic = null;
//							if(pic.getPixelType() != IPixelFormat.Type.RGB24){
//								//throw new IllegalArgumentException("pixelformat was not useful.. it was: " + pic.getPixelType());
//								newPic = IVideoPicture.make(IPixelFormat.Type.RGB24, videoCoder.getWidth(), videoCoder.getHeight());
//								if(resampler.resample(newPic, pic) <0){
//									throw new IllegalArgumentException("error hald");
//								}
//								
//							} else {
//								newPic = pic;
//							}
//							BufferedImage javaImg = com.xuggle.xuggler.Utils.videoPictureToImage(newPic);
//							
//						}
//					}
				}
			}
			videoCoder.close();
			c.close();
		} catch (Exception e) {
		}
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}
